{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How does the SWR vary along a line?\n",
    "Let's assume a 10 MHz source powering an antenna (of load $Z_L$) through a transmission line of length $L$. Depending on the location of the SWR-meter, what does one would read? \n",
    "\n",
    "This notebook is inspired from the reference: [\"Facts About SWR, Reflected Power, and Power Transfer on Real Transmission Lines with Loss\"](https://www.fars.k6ya.org/docs/Facts-about-SWR-and-Loss.pdf) by Steve Stearns given at ARRL Pacificon Antenna Seminar in 2010.\n",
    "\n",
    "<img src=\"Impedance_matching_4.svg\">\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let solve this question using `scikit-rf`. But first, the traditional Python imports:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "import skrf as rf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rf.stylely()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lossless lines\n",
    "Let's start with a lossless line of propagation constant $\\gamma=j\\beta$ and characteristic impedance $z_0=50\\Omega$ (real). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "freq = rf.Frequency(10, unit='MHz', npoints=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load and line properties\n",
    "Z_L = 75  # Ohm\n",
    "Z_0 = 50  # Ohm\n",
    "L = 50  # m\n",
    "\n",
    "# propagation constant\n",
    "beta = freq.w/rf.c\n",
    "gamma = 1j*beta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below we calculate the SWR of the line as a function of $z$ the line length measured from the load ($z=0$ at the load, $z=L$ at the source).  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "z = np.linspace(start=L, stop=0, num=301)\n",
    "SWRs = rf.zl_2_swr(z0=Z_0, zl=rf.zl_2_zin(Z_0, Z_L, gamma*z))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.plot(z, SWRs, lw=2)\n",
    "ax.set_xlabel('z [m]')\n",
    "ax.set_ylabel('SWR')\n",
    "ax.set_title('SWR along the (lossless) line')\n",
    "ax.invert_xaxis()\n",
    "ax.axvline(0, lw=8, color='k')\n",
    "ax.axvline(L, lw=8, color='k')\n",
    "ax.annotate('Load', xy=(0, 1.55), xytext=(10, 1.575),\n",
    "            arrowprops=dict(facecolor='black', shrink=0.05),\n",
    "            )\n",
    "ax.annotate('Source', xy=(50, 1.55), xytext=(40, 1.575),\n",
    "            arrowprops=dict(facecolor='black', shrink=0.05),\n",
    "            )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As expected, the SWR is the same everywhere along the line as the forward and backward wave amplitudes are also the same along the line.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lossy Lines\n",
    "Let's take the previous example but this time on a lossy line. The line is defined with a propagation constant $\\gamma=\\alpha + j\\beta$ :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = 0.01  # Np/m. Here a dummy value, just for the sake of the example\n",
    "gamma = alpha + 1j*beta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "z = np.linspace(0, L, num=101)\n",
    "SWRs = rf.zl_2_swr(z0=Z_0, zl=rf.zl_2_zin(Z_0, Z_L, gamma*z))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.plot(z, SWRs, lw=2)\n",
    "ax.set_xlabel('z [m]')\n",
    "ax.set_ylabel('SWR')\n",
    "ax.set_title('SWR along the (lossy) line')\n",
    "ax.invert_xaxis()\n",
    "ax.axvline(0, lw=8, color='k')\n",
    "ax.axvline(L, lw=8, color='k')\n",
    "ax.annotate('Load', xy=(0, 1.15), xytext=(10, 1.2),\n",
    "            arrowprops=dict(facecolor='black', shrink=0.05),\n",
    "            )\n",
    "ax.annotate('Source', xy=(50, 1.4), xytext=(40, 1.5),\n",
    "            arrowprops=dict(facecolor='black', shrink=0.05),\n",
    "            )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a lossy line, the SWR is maximum at the load and decreases to be minimum at the source side. \n",
    "\n",
    "Let's see how the impedance varies along the line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Zins = rf.zl_2_zin(Z_0, Z_L, gamma*z)\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.plot(z, np.abs(Zins/Z_0), lw=2, label='$Z/z_0$')\n",
    "ax.plot(z, SWRs, lw=2, ls='--', label=r'$SWR$')\n",
    "ax.plot(z, 1/SWRs, lw=2, ls='--', label=r'$1/SWR$')\n",
    "\n",
    "ax.set_xlabel('z [m]')\n",
    "ax.set_ylabel('Z (normalized to $z_0$)')\n",
    "ax.set_title('Impedance along the line')\n",
    "ax.invert_xaxis()\n",
    "ax.axvline(0, lw=8, color='k')\n",
    "ax.axvline(L, lw=8, color='k')\n",
    "ax.annotate('Load', xy=(0, 1.2), xytext=(10, 1.275),\n",
    "            arrowprops=dict(facecolor='black', shrink=0.05),\n",
    "            )\n",
    "ax.annotate('Source', xy=(50, 0.6), xytext=(40, 0.7),\n",
    "            arrowprops=dict(facecolor='black', shrink=0.05),\n",
    "            )\n",
    "ax.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The previous result is due to the fact that voltages and currents vary along the transmission line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "V_s = 1\n",
    "Z_in = rf.zl_2_zin(Z_0, Z_L, gamma*z)\n",
    "# Z_s = Z_0\n",
    "V_in = V_s * Z_in/(Z_0 + Z_in)\n",
    "I_in = V_in/(Z_0 + Z_in)\n",
    "# note that here we are going from source to load\n",
    "V, I = rf.voltage_current_propagation(V_in, I_in, Z_0, gamma*z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(2,1,sharex=True)\n",
    "ax[0].plot(z, np.abs(V), lw=2)\n",
    "ax[1].plot(z, np.abs(I), lw=2, color='C1')\n",
    "ax[1].set_xlabel('z [m]')\n",
    "ax[0].set_ylabel('|V| (V)')\n",
    "ax[1].set_ylabel('|I| (A)')\n",
    "ax[0].set_title('Voltage')\n",
    "ax[1].set_title('Current')\n",
    "[a.axvline(0, lw=8, color='k') for a in ax]\n",
    "[a.axvline(L, lw=8, color='k') for a in ax]\n",
    "ax[1].annotate('Load', xy=(50, 0.0075), xytext=(40, 0.0075),\n",
    "            arrowprops=dict(facecolor='black', shrink=0.05))\n",
    "ax[1].annotate('Source', xy=(0, 0.001), xytext=(10, 0.001),\n",
    "            arrowprops=dict(facecolor='black', shrink=0.05))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
